/**
 * class Enum
 * 只支持对象{key:value}形式且vaule只支持基本数据类型，不支持对象
 */
export default class Enum {
  constructor(data = {}) {
    return this.loopProxy(data);
  }
  loopProxy(data) {
    const _this = this;
    const proxy = new Proxy(data, {
      get(target, key) {
        const value = target[key];
        if (typeof value === 'function') {
          return value;
        } else if (typeof value === 'object') {
          return _this.loopProxy(value);
        } else if (value !== undefined) {
          return key;
        }
      },
      set(target, key, value) {
        if (target[key] || !value) {
          throw Error('已经存在枚举对象了，不能设置');
        }
        return Reflect.set(target, key, value);
      },
      defineProperty(target, key, desc) {
        if (desc && 'value' in desc) {
          target[key] = desc.value(target);
        }
        return target;
      }
    });

    // 定义获取enum值的方法
    Object.defineProperty(proxy, 'value', {
      value: target => key => target[key]
    });

    return proxy;
  }
}
